﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OtherApi.Filter
{
    public class LowercaseContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName.ToLower();
        }
    }
    public class CustomActionJsonFormatAttribute : ActionFilterAttribute
    {
        //private Type _ContractResolver { get; set; }
        public CustomActionJsonFormatAttribute()//Type ContractResolver
        {
           // _ContractResolver = ContractResolver;
        }
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            var jsonResult = (JsonResult)actionExecutedContext.Result;
            jsonResult.SerializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()//Activator.CreateInstance(_ContractResolver) as IContractResolver
            }; 
            actionExecutedContext.Result = jsonResult;
        }
    }
}
